package com.meridian.commerce.web;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.meridian.commerce.model.UserAccount;
import com.meridian.commerce.model.UserAccountAddress;

@Controller
public class UserAccountAddressController extends AbstractCrudController<UserAccountAddress> {

	@RequestMapping(method = RequestMethod.GET)
	public String setup(@RequestParam(value = "id", required = false) Integer id) {

		System.out.println("start2: " + id);

		return "index";
	}

	@ModelAttribute("useraccounts")
	public Collection<UserAccount> populateUserAccounts() {
		return UserAccount.findAllUserAccounts();
	}
	@ModelAttribute("states")
	public Map<String, String> getStateList() {
		Map<String, String> stateMap = new LinkedHashMap<String, String>();
		stateMap.put("", "Select State");
		stateMap.put("AA", "Armed Forces Americas");
		stateMap.put("AE", "Armed Forces Africa, Canada, Europe, Middle East");
		stateMap.put("AK", "Alaska");
		stateMap.put("AL", "Alabama");
		stateMap.put("AP", "Armed Forces Pacific");
		stateMap.put("AR", "Arkansas");
		stateMap.put("AS", "American Samoa");
		stateMap.put("AZ", "Arizona");
		stateMap.put("CA", "California");
		stateMap.put("CO", "Colorado");
		stateMap.put("CT", "Connecticut");
		stateMap.put("DC", "District of Columbia");
		stateMap.put("DE", "Delaware");
		stateMap.put("FL", "Florida");
		stateMap.put("GA", "Georgia");
		stateMap.put("GU", "Guam");
		stateMap.put("HI", "Hawaii");
		stateMap.put("IA", "Iowa");
		stateMap.put("ID", "Idaho");
		stateMap.put("IL", "Illinois");
		stateMap.put("IN", "Indiana");
		stateMap.put("KS", "Kansas");
		stateMap.put("KY", "Kentucky");
		stateMap.put("LA", "Louisiana");
		stateMap.put("MA", "Massachusetts");
		stateMap.put("MD", "Maryland");
		stateMap.put("ME", "Maine");
		stateMap.put("MI", "Michigan");
		stateMap.put("MN", "Minnesota");
		stateMap.put("MO", "Missouri");
		stateMap.put("MS", "Mississippi");
		stateMap.put("MT", "Montana");
		stateMap.put("NC", "North Carolina");
		stateMap.put("ND", "North Dakota");
		stateMap.put("NE", "Nebraska");
		stateMap.put("NH", "New Hampshire");
		stateMap.put("NJ", "New Jersey");
		stateMap.put("NM", "New Mexico");
		stateMap.put("NV", "Nevada");
		stateMap.put("NY", "New York");
		stateMap.put("OH", "Ohio");
		stateMap.put("OK", "Oklahoma");
		stateMap.put("OR", "Oregon");
		stateMap.put("PA", "Pennsylvania");
		stateMap.put("PR", "Puerto Rico");
		stateMap.put("PW", "Palau");
		stateMap.put("RI", "Rhode Island");
		stateMap.put("SC", "South Carolina");
		stateMap.put("SD", "South Dakota");
		stateMap.put("TN", "Tennessee");
		stateMap.put("TX", "Texas");
		stateMap.put("UT", "Utah");
		stateMap.put("VA", "Virginia");
		stateMap.put("VI", "U.S. Virgin Islands");
		stateMap.put("VT", "Vermont");
		stateMap.put("WA", "Washington");
		stateMap.put("WI", "Wisconsin");
		stateMap.put("WV", "West Virginia");
		stateMap.put("WY", "Wyoming");

		return stateMap;
	}

	@Autowired
	private GenericConversionService conversionService;



	Converter<UserAccount, String> getUserAccountConverter() {
		return new Converter<UserAccount, String>() {
			public String convert(UserAccount userAccount) {
				return new StringBuilder().append(userAccount.getFirstName())
						.append(" ").append(userAccount.getMiddleName())
						.append(" ").append(userAccount.getLastName())
						.toString();
			}
		};
	}

	Converter<UserAccountAddress, String> getUserAccountAddressConverter() {
		return new Converter<UserAccountAddress, String>() {
			public String convert(UserAccountAddress userAccountAddress) {
				return new StringBuilder()
						.append(userAccountAddress.getFullName()).append(" ")
						.append(userAccountAddress.getAddressLine1())
						.append(" ")
						.append(userAccountAddress.getAddressLine2())
						.toString();
			}
		};
	}

	@PostConstruct
	void registerConverters() {
		conversionService.addConverter(getUserAccountConverter());
		conversionService.addConverter(getUserAccountAddressConverter());
	}
}
